/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Collections;
/*     */ import java.util.EnumMap;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class ChangePasswordListener
/*     */ {
/*     */   public static enum InternalChangePasswordListenerId
/*     */   {
/*  38 */     CPL_SYNC;
/*     */     
/*     */ 
/*     */     private InternalChangePasswordListenerId() {}
/*     */   }
/*     */   
/*     */ 
/*  45 */   private static Map<InternalChangePasswordListenerId, ChangePasswordListener> mInternalListeners = Collections.synchronizedMap(new EnumMap(InternalChangePasswordListenerId.class));
/*     */   
/*     */ 
/*  48 */   private static Map<String, ChangePasswordListener> mExternalListeners = Collections.synchronizedMap(new HashMap());
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void register(String listenerName, ChangePasswordListener listener)
/*     */   {
/*  58 */     ChangePasswordListener obj = (ChangePasswordListener)mExternalListeners.get(listenerName);
/*  59 */     if (obj != null) {
/*  60 */       ZimbraLog.account.warn("listener name " + listenerName + " is already registered, " + "registering of " + obj.getClass().getCanonicalName() + " is ignored");
/*     */       
/*  62 */       return;
/*     */     }
/*     */     
/*  65 */     mExternalListeners.put(listenerName, listener);
/*     */   }
/*     */   
/*     */   public static void registerInternal(InternalChangePasswordListenerId listenerEnum, ChangePasswordListener listener)
/*     */   {
/*  70 */     ChangePasswordListener obj = (ChangePasswordListener)mInternalListeners.get(listenerEnum);
/*  71 */     if (obj != null) {
/*  72 */       ZimbraLog.account.warn("listener " + listenerEnum + " is already registered, " + "registering of " + obj.getClass().getCanonicalName() + " is ignored");
/*     */       
/*  74 */       return;
/*     */     }
/*     */     
/*  77 */     mInternalListeners.put(listenerEnum, listener);
/*     */   }
/*     */   
/*     */   private static ChangePasswordListener getHandler(Account acct) throws ServiceException {
/*  81 */     Domain domain = Provisioning.getInstance().getDomain(acct);
/*  82 */     if (domain == null) {
/*  83 */       throw AccountServiceException.NO_SUCH_DOMAIN(acct.getDomainName());
/*     */     }
/*  85 */     String listenerName = domain.getAttr("zimbraPasswordChangeListener");
/*     */     
/*  87 */     if (listenerName == null) {
/*  88 */       return null;
/*     */     }
/*  90 */     ChangePasswordListener listener = (ChangePasswordListener)mExternalListeners.get(listenerName);
/*  91 */     if (listener == null) {
/*  92 */       throw ServiceException.FAILURE("change password listener " + listenerName + " for account " + acct.getName() + " not found", null);
/*     */     }
/*  94 */     return listener;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static class ChangePasswordListenerContext
/*     */   {
/* 101 */     Map<ChangePasswordListener.InternalChangePasswordListenerId, Map<String, Object>> mInternalCtxts = new EnumMap(ChangePasswordListener.InternalChangePasswordListenerId.class);
/*     */     
/*     */ 
/* 104 */     ChangePasswordListener mExternalListener = null;
/* 105 */     Map<String, Object> mExternalCtxt = null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void invokePreModify(Account acct, String newPassword, ChangePasswordListenerContext ctxts, Map<String, Object> attrsToModify)
/*     */     throws ServiceException
/*     */   {
/* 113 */     for (Map.Entry<InternalChangePasswordListenerId, ChangePasswordListener> listener : mInternalListeners.entrySet()) {
/* 114 */       InternalChangePasswordListenerId listenerEnum = (InternalChangePasswordListenerId)listener.getKey();
/* 115 */       ChangePasswordListener listenerInstance = (ChangePasswordListener)listener.getValue();
/* 116 */       Map<String, Object> context = new HashMap();
/* 117 */       ctxts.mInternalCtxts.put(listenerEnum, context);
/* 118 */       listenerInstance.preModify(acct, newPassword, context, attrsToModify);
/*     */     }
/*     */     
/*     */ 
/* 122 */     ChangePasswordListener cpListener = getHandler(acct);
/* 123 */     if (cpListener != null) {
/* 124 */       ctxts.mExternalListener = cpListener;
/*     */       
/* 126 */       Map<String, Object> context = new HashMap();
/* 127 */       ctxts.mExternalCtxt = context;
/* 128 */       cpListener.preModify(acct, newPassword, context, attrsToModify);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void invokePostModify(Account acct, String newPassword, ChangePasswordListenerContext ctxts)
/*     */   {
/* 134 */     for (Map.Entry<InternalChangePasswordListenerId, ChangePasswordListener> listener : mInternalListeners.entrySet()) {
/* 135 */       InternalChangePasswordListenerId listenerEnum = (InternalChangePasswordListenerId)listener.getKey();
/* 136 */       ChangePasswordListener listenerInstance = (ChangePasswordListener)listener.getValue();
/* 137 */       Map<String, Object> context = (Map)ctxts.mInternalCtxts.get(listenerEnum);
/* 138 */       listenerInstance.postModify(acct, newPassword, context);
/*     */     }
/*     */     
/* 141 */     if (ctxts.mExternalListener != null) {
/* 142 */       ctxts.mExternalListener.postModify(acct, newPassword, ctxts.mExternalCtxt);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static void invokeOnException(Account acct, String newPassword, ChangePasswordListenerContext ctxts, ServiceException exceptionThrown)
/*     */   {
/* 149 */     for (Map.Entry<InternalChangePasswordListenerId, ChangePasswordListener> listener : mInternalListeners.entrySet()) {
/* 150 */       InternalChangePasswordListenerId listenerEnum = (InternalChangePasswordListenerId)listener.getKey();
/* 151 */       ChangePasswordListener listenerInstance = (ChangePasswordListener)listener.getValue();
/* 152 */       Map<String, Object> context = (Map)ctxts.mInternalCtxts.get(listenerEnum);
/* 153 */       listenerInstance.onException(acct, newPassword, context, exceptionThrown);
/*     */     }
/*     */     
/* 156 */     if (ctxts.mExternalListener != null) {
/* 157 */       ctxts.mExternalListener.onException(acct, newPassword, ctxts.mExternalCtxt, exceptionThrown);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public abstract void preModify(Account paramAccount, String paramString, Map paramMap, Map<String, Object> paramMap1)
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public abstract void postModify(Account paramAccount, String paramString, Map paramMap);
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void onException(Account acct, String newPassword, Map context, ServiceException exceptionThrown) {}
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static class DummyChangePasswordListener
/*     */     extends ChangePasswordListener
/*     */   {
/*     */     public void preModify(Account acct, String newPassword, Map context, Map<String, Object> attrsToModify)
/*     */       throws ServiceException
/*     */     {
/* 201 */       System.out.println("preModify dummy");
/*     */     }
/*     */     
/*     */     public void postModify(Account acct, String newPassword, Map context) {
/* 205 */       System.out.println("postModify dummy");
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 212 */     registerInternal(InternalChangePasswordListenerId.CPL_SYNC, new DummyChangePasswordListener());
/*     */     
/*     */ 
/* 215 */     register("dummy", new DummyChangePasswordListener());
/*     */     
/* 217 */     Provisioning prov = Provisioning.getInstance();
/* 218 */     Account acct = prov.get(Key.AccountBy.name, "user1");
/*     */     
/*     */ 
/* 221 */     Domain domain = prov.getDomain(acct);
/* 222 */     Map attrs = new HashMap();
/* 223 */     attrs.put("zimbraPasswordChangeListener", "dummy");
/* 224 */     prov.modifyAttrs(domain, attrs);
/*     */     
/* 226 */     prov.changePassword(acct, "test123", "test123-new");
/*     */     
/*     */ 
/* 229 */     attrs.clear();
/* 230 */     attrs.put("zimbraPasswordChangeListener", "");
/* 231 */     prov.modifyAttrs(domain, attrs);
/*     */     
/*     */ 
/* 234 */     prov.changePassword(acct, "test123-new", "test123");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/ChangePasswordListener.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */